package view.carte;

import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.JLabel;

import rmi.RemoteObserver;
import rmi.UtilitairesRmi;
import server.models.posteDeTir.IPosteDeTir;
import server.models.tireur.ITireur;

public class PosteDeTirView extends JLabel implements RemoteObserver {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String nomPosteDeTir = "";

	public PosteDeTirView(IPosteDeTir posteDeTir, String nomClient) {
		super();
		this.nomPosteDeTir = "";
		try {
			posteDeTir.addObserver((RemoteObserver)UtilitairesRmi.getInstance().obtenirEtBind("PosteDeTirView_" + posteDeTir.getNumero() + "_" + nomClient, this));
		} catch (RemoteException | AlreadyBoundException
				| NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setText(this.nomPosteDeTir);
		this.setVisible(true);
		this.repaint();
	}

	@Override
	public void update(Object observable, Object arg) {
		IPosteDeTir posteDeTir = (IPosteDeTir)observable;

		String text = this.nomPosteDeTir;
		try {
			for(ITireur tireur : posteDeTir.getListeTireurs()){
				text += tireur.getSurnom() + " ";				
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		this.setText(text);

		this.repaint();
	}
}